package com.excellence.controller;

import com.excellence.datamodel.Result;
import com.excellence.pojo.Crop;
import com.excellence.pojo.Operation;
import com.excellence.service.OperationService;
import com.excellence.service.impl.CropServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 沈秉璋
 * @ClassName OperationController
 * @Description TODO
 * @date 2024/4/8 11:40
 * @Version 1.0
 */
//@Controller
//@ResponseBody
@RestController
@RequestMapping("/api/operation")
public class OperationController {
    @Autowired
    private OperationService operationService;
    @Autowired
    private CropServiceImpl cropService;

    @DeleteMapping("/deleteByPrimaryKey")
    Result deleteByPrimaryKey(Long id) {
        int res = operationService.deleteByPrimaryKey(id);
        return Result.success(res);
    }

    @PostMapping("/insert")
    Result insert(@RequestBody Operation record) {
        int res = operationService.insert(record);
        return Result.success(res);
    }

    @PostMapping("/insertSelective")
    Result insertSelective(Operation record, @RequestParam(value = "files", required = false) List<MultipartFile> files) {
        int res = operationService.insertSelective(record, files);
        return Result.success(res);
    }

    @GetMapping("/selectByUserId")
    Result selectByPrimaryKey(Long userId) {
        System.out.println(userId);
        List<Operation> res = operationService.selectByUserId(userId);
        return Result.success(res);
    }

    @GetMapping("/selectAll")
    Result selectAll() {
        List<Operation> res = operationService.selectAll();
        return Result.success(res);
    }

    @GetMapping("/count")
    Result count(Integer massifId) {
        List<Crop> crops = cropService.selectByMassifId(massifId);
        int count = 0;
        for (Crop crop : crops) {
            count = operationService.count(crop.getCropId());
        }
        return Result.success(count);
    }

    @PutMapping("/updateByPrimaryKeySelective")
    Result updateByPrimaryKeySelective(Operation record, @RequestParam(value = "files", required = false) List<MultipartFile> files) {
        int res = operationService.updateByPrimaryKeySelective(record, files);
        return Result.success(res);
    }

    @PutMapping("/updateByPrimaryKey")
    Result updateByPrimaryKey(Operation record, @RequestParam(value = "files", required = false) List<MultipartFile> files) {
        int res = operationService.updateByPrimaryKey(record, files);
        return Result.success(res);
    }
}
